from flask import Blueprint, render_template, jsonify, request
from flask_login import login_required, current_user
from applications.common.utils.rights import authorize
from applications.models import Share
from applications.schemas import ShareSchema
from sqlalchemy import desc
from applications.common.utils.http import table_api, fail_api, success_api
from applications.common.curd import get_one_by_id
from applications.common.utils.validate import str_escape
from applications.extensions import db
import datetime

bp = Blueprint('list', __name__, url_prefix='/list')


# 首页
@bp.get('/')
def index():
    return render_template('front/list/main.html')

# 资源数据
@bp.get('/data')
def data():
    limit = str_escape(request.args.get('limit', type=int))
    if int(limit) > 1000 or int(limit) < 0:
        limit = 100
    share_name = str_escape(request.args.get('shareName', type=str))
    filters = [Share.is_del==0,Share.icon!="folder"]
    if share_name:
        filters.append(Share.name.contains(share_name))
    shares = Share.query.filter(*filters).order_by(desc(Share.id)).layui_paginate()
    return table_api(data=ShareSchema(many=True).dump(shares), count=shares.total,limit=limit)
